/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUIv2;

import general.ActionEnum;
import general.FileUtils;
import general.GlobalProperties;
import jsonObjects.Attributes;
import jsonObjects.Profile;
import org.apache.log4j.Logger;
import systemFile.FileBuffer;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class FileAttributesEditGUI extends javax.swing.JPanel {
    private static final long serialVersionUID = -3624351081270106962L;
    private static Logger log = Logger.getLogger(FileAttributesEditGUI.class);

    public FileAttributesEditGUI(File file, Profile profile) {
        this.profile = profile;
        this.file = file;
        initComponents();
    }
    
    @SuppressWarnings("serial")
    private void initComponents() {
        Map<Attributes, String> mapa = FileUtils.getAtrybutyPliku(
                file.getName(), profile);

        Object[][] futureMenuItems;
        if (mapa != null && !mapa.isEmpty()) {
            int i = 0;
            futureMenuItems = new Object[mapa.size()][2];

            for (Attributes a : mapa.keySet()) {
                futureMenuItems[i][0] = a.getName();
                futureMenuItems[i][1] = mapa.get(a);
                i++;
            }
        } else
            futureMenuItems = new Object[0][0];

        cancelButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        cancelButton.setText("Return");
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        nextButton.setText("Save");
        nextButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                futureMenuItems,
                new String[]{
                        "Name", "Value"
                }
        ) {
            boolean[] canEdit = new boolean[]{
                    false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup()
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(0, 0, Short.MAX_VALUE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(cancelButton).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(nextButton))).addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(nextButton).addComponent(cancelButton)).addContainerGap())
        );
    }

    private void jButton2MouseClicked(MouseEvent evt) {
        Map<String, String> mapa = new HashMap<String, String>();
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            String a = String.valueOf(jTable1.getValueAt(i, 0));
            String b = String.valueOf(jTable1.getValueAt(i, 1));
            mapa.put(a, b);
        }

        FileOutputStream fos = null;
        try {
            File propFile = FileUtils.getProfilePropertyFile(profile);
            GlobalProperties.unhide(propFile);

            fos = new FileOutputStream(propFile);
            Properties properties = FileUtils.loadProperties(propFile);
            String tmp;
            for (String key : mapa.keySet()) {
                tmp = file.getName() + "?" + key;
                properties.setProperty(tmp, mapa.get(key));
            }

            properties.store(fos, "");

            if (!propFile.isHidden())
                GlobalProperties.hide(propFile);
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }

        JOptionPane.showMessageDialog(null, "Niektore zmiany beda widoczne dopiero po ponownym uruchomieniu programu!");

        if (FileUtils.getProfilePropertyFile(profile).exists())
            FileBuffer.getInstance().getListOfFile().put(FileUtils.getProfilePropertyFile(profile), ActionEnum.EDIT);


        FileAttributeReplacePanel replacePanel = new FileAttributeReplacePanel();

        javax.swing.JPanel parenPanel = (javax.swing.JPanel) jScrollPane1.getParent();
        parenPanel.removeAll();

        javax.swing.GroupLayout parenPanelLayot = new javax.swing.GroupLayout(parenPanel);
        parenPanel.setLayout(parenPanelLayot);
        parenPanelLayot.setHorizontalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        parenPanelLayot.setVerticalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
        );
        parenPanel.add(replacePanel);
        parenPanel.revalidate();
        parenPanel.repaint();
    }

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
        FileAttributeReplacePanel replacePanel = new FileAttributeReplacePanel();

        javax.swing.JPanel parenPanel = (javax.swing.JPanel) jScrollPane1.getParent();
        parenPanel.removeAll();

        javax.swing.GroupLayout parenPanelLayot = new javax.swing.GroupLayout(parenPanel);
        parenPanel.setLayout(parenPanelLayot);
        parenPanelLayot.setHorizontalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        parenPanelLayot.setVerticalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
        );
        parenPanel.add(replacePanel);
        parenPanel.revalidate();
        parenPanel.repaint();
    }

    javax.swing.JButton cancelButton;
    javax.swing.JScrollPane jScrollPane1;
    javax.swing.JTable jTable1;
    javax.swing.JButton nextButton;
    File file;
    Profile profile;
}
